#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

int cal(vector<vector<int>>& adj, vector<int> &num, int u)
{
        int max_dl = num[u];
        for(int v : adj[u]){
                max_dl = max(max_dl, cal(adj, num, v) + num[u]);
        }
        return max_dl;
}

int main()
{
        int n;
        cin >> n;
        vector<int> nums(n + 1);
        vector<vector<int>> adj(n+1);

        for(int i = 1; i <=n;  i++){
                cin >> nums[i];
        }
        while(true){
                int x, y;
                cin >> x >> y;
                if(x == 0 && y == 0) break;
                adj[x].push_back(y);
        }

        vector<int> dp(n + 1, 0);
        int max_dl = 0;
        for(int i = 1; i <=n; i++){
                max_dl = max(max_dl, cal(adj, nums, i));
        }
        cout<<max_dl << endl;
        return 0;
}